<template>
  <div>
    <div class="filter-container">
      <el-button type="primary" icon="el-icon-plus" size="small" @click="handleCreate">新增</el-button>
    </div>
    <el-table :data="tableData" style="width: 100%" border size="mini">
      <el-table-column prop="name" label="角色名" width="180">
      </el-table-column>
      <el-table-column label="功能权限">
        <template slot-scope="scope">
          <el-tag close-transition v-for="item in scope.row.permissions.data" :key="item.id" class="el-tag" size="mini">{{item.name|statusPermissions}}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="180">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button size="mini" type="danger" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog :title="dialogMode==='create'?'新增':'修改'" :visible.sync="dialogVisible">
      <el-form ref="roleInfo" :model="roleInfo" :rules="rules" label-width="80px">
        <el-form-item label="角色名" prop="name">
          <el-input v-model="roleInfo.name"></el-input>
        </el-form-item>
        <el-form-item label="功能权限" prop="permissions">
          <el-transfer v-model="roleInfo.permissions" :data="permissionsData" :titles="['系统功能','已有功能']"></el-transfer>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleSave">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script type="text/ecmascript-6">
import {
  getRoles,
  getPermissions,
  updateRole,
  createRole,
  deleteRole
} from 'api/role'
export default {
  data() {
    return {
      tableData: null,
      permissionsData: [],
      dialogVisible: false,
      dialogMode: '',
      roleInfo: {
        id: '',
        name: '',
        promissions: []
      },
      rules: {
        name: [{ required: true, message: '请输入角色名', trigger: 'blur' }],
        permissions: [
          {
            type: 'array',
            required: true,
            message: '请至少选择一个系统功能',
            trigger: 'change'
          }
        ]
      }
    }
  },
  methods: {
    handleCreate() {
      this.dialogMode = 'create'
      this.dialogVisible = true
    },
    handleEdit(index, row) {
      this.dialogMode = 'edit'
      this.roleInfo = Object.assign({}, row)
      let permissions = this.roleInfo.permissions.data
      this.roleInfo.permissions = permissions.map(permission => permission.id)
      this.dialogVisible = true
    },
    handleDelete(index, row) {
      deleteRole(row.id).then(() => {
        this.$notify({
          title: '成功',
          message: '删除成功',
          type: 'success',
          duration: 2000
        })
        const index = this.tableData.indexOf(row)
        this.tableData.splice(index, 1)
      })
    },
    handleSave() {
      if (this.dialogMode === 'create') {
        const { ...data } = this.roleInfo
        delete data.id
        createRole(data).then(response => {
          this.tableData.unshift(response)
          this.dialogVisible = false
          this.$notify({
            title: '成功',
            message: '创建成功',
            type: 'success',
            duration: 2000
          })
        })
      } else {
        const id = this.roleInfo.id
        const { ...data } = this.roleInfo
        delete data.id
        updateRole(id, data).then(response => {
          for (const item of this.tableData) {
            if (item.id === response.id) {
              const index = this.tableData.indexOf(item)
              this.tableData.splice(index, 1, response)
              break
            }
          }
          this.dialogVisible = false
          this.$notify({
            title: '成功',
            message: '更新成功',
            type: 'success',
            duration: 2000
          })
        })
      }
    }
  },
  created() {
    getRoles().then(response => {
      const data = response.data
      this.tableData = data
    })
    getPermissions().then(response => {
      const data = response.data
      data.forEach((item, index) => {
        this.permissionsData.push({
          label: this.$options.filters['statusPermissions'](item.name),
          key: item.id
        })
      })
    })
  },
  filters: {
    statusPermissions(status) {
      const statusMap = {
        edit_settings: '系统设置',
        confirm_new_students: '现场确认',
        manage_users: '人员管理',
        manage_new_students: '新生管理'
      }
      return statusMap[status]
    }
  }
}
</script>

<style scoped lang="stylus">
.el-tag
  margin-right 8px
</style>
